package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Question;
import com.ruoyi.system.service.IQuestionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.QQuestionnaire;
import com.ruoyi.system.service.IQQuestionnaireService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问卷信息主Controller
 *
 * @author ruoyi
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/system/questionnaire")
public class QQuestionnaireController extends BaseController {
    @Autowired
    private IQQuestionnaireService qQuestionnaireService;
    @Autowired
    private IQuestionService questionService;

    /**
     * 查询问卷信息主列表
     */
    @PreAuthorize("@ss.hasPermi('system:questionnaire:list')")
    @GetMapping("/list")
    public TableDataInfo list(QQuestionnaire qQuestionnaire) {
        startPage();
        List<QQuestionnaire> list = qQuestionnaireService.selectQQuestionnaireList(qQuestionnaire);
        return getDataTable(list);
    }

    @GetMapping("/find")
    public HashMap find(Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        QQuestionnaire aa = qQuestionnaireService.selectQQuestionnaireById(id);
        map.put("questionnaireinfo", aa);
        ArrayList<Object> questionlist = new ArrayList<>();
        Question a1 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion1Name()));
        questionlist.add(a1);
        Question a2 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion2Name()));
        questionlist.add(a2);
        Question a3 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion3Name()));
        questionlist.add(a3);
        Question a4 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion4Name()));
        questionlist.add(a4);
        Question a5 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion5Name()));
        questionlist.add(a5);
        Question a6 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion6Name()));
        questionlist.add(a6);
        Question a7 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion7Name()));
        questionlist.add(a7);
        Question a8 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion8Name()));
        questionlist.add(a8);
        Question a9 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion9Name()));
        questionlist.add(a9);
        Question a10 = questionService.selectQuestionById(Long.parseLong(aa.getQuestion10Name()));
        questionlist.add(a10);
        map.put("questionlist", questionlist);
        return map;
    }


    /**
     * 导出问卷信息主列表
     */
    @PreAuthorize("@ss.hasPermi('system:questionnaire:export')")
    @Log(title = "问卷信息主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QQuestionnaire qQuestionnaire) {
        List<QQuestionnaire> list = qQuestionnaireService.selectQQuestionnaireList(qQuestionnaire);
        ExcelUtil<QQuestionnaire> util = new ExcelUtil<QQuestionnaire>(QQuestionnaire.class);
        util.exportExcel(response, list, "问卷信息主数据");
    }

    /**
     * 获取问卷信息主详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:questionnaire:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(qQuestionnaireService.selectQQuestionnaireById(id));
    }

    /**
     * 新增问卷信息主
     */
    @PreAuthorize("@ss.hasPermi('system:questionnaire:add')")
    @Log(title = "问卷信息主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QQuestionnaire qQuestionnaire) {
        return toAjax(qQuestionnaireService.insertQQuestionnaire(qQuestionnaire));
    }

    /**
     * 修改问卷信息主
     */
    @PreAuthorize("@ss.hasPermi('system:questionnaire:edit')")
    @Log(title = "问卷信息主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QQuestionnaire qQuestionnaire) {
        return toAjax(qQuestionnaireService.updateQQuestionnaire(qQuestionnaire));
    }

    /**
     * 删除问卷信息主
     */
    @PreAuthorize("@ss.hasPermi('system:questionnaire:remove')")
    @Log(title = "问卷信息主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(qQuestionnaireService.deleteQQuestionnaireByIds(ids));
    }
}
